<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    //订单列表页
    public function getIndex(Request $request){

        //获取搜索id
        $search = $request->input('search');

        //获取页数id
        $num = $request->input('num',10);

        //判断搜索  模糊查询
        if(!empty($search)){
            $data =DB::table('order')->where('oid','like','%'.$search.'%')->paginate($num);
        }else{
            $data = DB::table('order')->paginate($num);
        }
        
        //引入模板  传输数据
        return view('admin.order.index',['data'=>$data,'request'=>$request->all()]);
    }

    //卖家发货操作
    public function getSend(Request $request){

        //获取id
        $id = $request->input('id');

        //当oid等于$id时  改变状态为1
        $res = DB::table('order')->where('oid',$id)->update(['ostatus'=>1]);

        //判断结果
        if($res>0){
            return redirect('/admin/order/index')->with('success','发货成功，等待收货');
        }else{
            return back()->with('error','发货失败');
        }
    }

      //删除操作
    public function getDelete(Request $request){

        //获取id
        $data = $request->input('id');

        //获取要删除的信息
        $res = DB::table('order')->where('oid',$data)->delete();

        //判断结果
        if($res>0){
            return redirect('/admin/order/index')->with('success','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }
    
    //订单详情操作
    public function getDetail(Request $request){
        $res = $request->input('id');
        $data = DB::table('order_detail')->join('goods','order_detail.gid','=','goods.gid')->where('did',$res)->get();
         //dd($data);
        $money = $data[0]["gprice"]*$data[0]["gnum"];
         
        return view('admin.order.detail',['data'=>$data,'money'=>$money]);
    }  
}

   
